//
//  ReadDetailViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/14.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "MainDataHelper.h"
#import "ReadModel.h"
#import "ReadListTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ReadDetailContent.h"
#import "MJRefresh.h"
#import "ReadDetailRightTableViewCell.h"
#import "ReadDetailRightViewController.h"

@interface ReadDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *listArray;

@property (nonatomic, strong) UITableView *rightTableView;

@property (nonatomic, strong) NSMutableArray *rightArray;

@property (nonatomic, strong) NSMutableDictionary *dataDictionary;

// 设置scrollview
@property (nonatomic, strong) UIScrollView *scrollView;

// 下拉刷新,上拉加载需要的属性
@property (nonatomic, assign) int pageNum;
@property (nonatomic, assign) int rightNum;

@end

@implementation ReadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageNum = 1;
    self.rightNum = 1;
    
    [self loadScrollView];
    [self loadTableView];
    [self loadData];
    [self setupRefresh];
    [self loadRightData];
    [self setupRightRefresh];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    //初始化一个UIBarButtonItem
    UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-new(1)"] style:UIBarButtonItemStylePlain target:self action:@selector(new)];
    
    UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-hot"] style:UIBarButtonItemStylePlain target:self action:@selector(hot)];
    
    //给右边的按钮一组视图,会自动排列
    self.navigationItem.rightBarButtonItems = @[buttonItem2,buttonItem1];
    
    
}

#pragma mark --- item的点击事件(实现点击item来切换不同tableView)
// 返回点击
- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
// new点击
- (void)new{
    
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

}
// hot点击
- (void)hot{
    
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
    
}


#pragma mark --- 设置底部的scrollView
- (void)loadScrollView{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    //是否整屏翻动
    _scrollView.pagingEnabled = YES;
    //是否设置边缘弹动效果
    _scrollView.bounces = NO;
    //指定代理人
    _scrollView.delegate = self;
    
    [self.view addSubview:_scrollView];
}



#pragma mark --- tableVIew设置

- (void)loadTableView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_scrollView addSubview:_tableView];
    UINib *listNib = [UINib nibWithNibName:@"ReadListTableViewCell" bundle:nil];
    [_tableView registerNib:listNib forCellReuseIdentifier:@"cell"];
    
    
    _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _rightTableView.dataSource = self;
    _rightTableView.delegate = self;
    [_scrollView addSubview:_rightTableView];
    UINib *rightlistNib = [UINib nibWithNibName:@"ReadDetailRightTableViewCell" bundle:nil];
    [_rightTableView registerNib:rightlistNib forCellReuseIdentifier:@"Cell"];
    
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
}

#pragma mark --- 数组懒加载
- (NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (NSMutableArray *)rightArray{
    
    if (!_rightArray) {
        _rightArray = [NSMutableArray array];
    }
    return _rightArray;
}


#pragma mark --- 第三方方法 上拉加载,下拉刷新方法
- (void)steputRefresh{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadData];
    }];
    
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        
        [self loadData];
    }];
}
// 右边的tableView上拉加载,下拉刷新方法
- (void)steputRightRefresh{
    [_rightArray removeAllObjects];
    self.rightTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        [self loadRightData];
         [_rightArray removeAllObjects];
    }];
    
    self.rightTableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        _rightNum += 10;
        [self loadRightData];
    }];
}


- (void)setupRefresh {
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pushReload)];
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(downReload)];
}

- (void)pushReload {
    [self loadData];
    [self.tableView.header beginRefreshing];
}

- (void)downReload {
    self.pageNum++;
    [self loadData];
    [self.tableView.footer beginRefreshing];
}



#pragma mark --- 获取数据
- (void)loadData
{
    NSString *string = @"http://api2.pianke.me/read/columns_detail";

//     self.listArray = [NSMutableArray array];


    NSString *bodyString = [NSString stringWithFormat:@"auth=W8Um3Eiixm3qtDC8h8GcDrlDJxQMeoN76M8GxpD2Qm3cWP22NFFQx1a7Ps&client=1&deviceid=70D20EF9-AB8B-42FD-937C-7D884DB58895&limit=%d&sort=addtime&start=0&typeid=%ld&version=3.0.6",self.pageNum*10,(long)self.typeid] ;
    [MainDataHelper postDataWithUrlString:string andBodyString:bodyString compare:^(id object) {
        self.listArray = [NSMutableArray array];

        
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"list"];
        if (array.count == 0) {
            return ;
        }
        for (NSDictionary *dic1 in array) {
            ReadModel *model = [[ReadModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.listArray addObject:model];
        }
        [self.tableView reloadData];
        
        // 结束下拉刷新
        [self.tableView.header endRefreshing];
        // 结束上拉加载
        [self.tableView.footer endRefreshing];
        
    }];
    
}

- (void)setupRightRefresh {
    self.rightTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pushRightReload)];
    self.rightTableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(downRightReload)];
}

- (void)pushRightReload {
    [self loadRightData];
    [self.rightTableView.header beginRefreshing];
}

- (void)downRightReload {
    self.rightNum++;
    [self loadRightData];
    [self.rightTableView.footer beginRefreshing];
}

- (void)loadRightData{

    
    
    

    NSString *rightstring = @"http://api2.pianke.me/read/columns_detail";
    NSString *rightbodyString = [NSString stringWithFormat:@"auth=W8Um3Eiixm3qtDC8h8GcDrlDJxQMeoN76M8GxpD2Qm3cWP22NFFQx1a7Ps&client=&deviceid=70D20EF9-AB8B-42FD-937C-7D884DB58895&limit=%d&sort=hot&start=0&typeid=%ld&version=3.0.6",self.rightNum*10,(long)self.typeid] ;
    [MainDataHelper postDataWithUrlString:rightstring andBodyString:rightbodyString compare:^(id object) {


        self.rightArray = [NSMutableArray array];

        
        NSDictionary *dic2 = object;
        NSArray *arr = dic2[@"data"][@"list"];
        if (arr.count == 0) {
            return ;
        }
        for (NSDictionary *dictionary in arr) {
            
            ReadModel *readmodel = [[ReadModel alloc] init];
            [readmodel setValuesForKeysWithDictionary:dictionary];
            [self.rightArray addObject:readmodel];
        }
        [self.rightTableView reloadData];
        
        // 结束下拉刷新
        [self.rightTableView.header endRefreshing];
        // 结束上拉加载
        [self.rightTableView.footer endRefreshing];
    }];
}


#pragma mark --- tableView部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        
        return self.listArray.count;
    }else{
        
        return self.rightArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {

        ReadListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        if (_listArray.count == 0) {
            return nil;
        }
        ReadModel *model = self.listArray[indexPath.row];
        cell.titleLabel.text = model.title;
        [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
        cell.contentLabel.text = model.content;
        return cell;
    }else{
        
        ReadDetailRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        if (_rightArray.count == 0) {
            return nil;
        }
        ReadModel *models = self.rightArray[indexPath.row];
        cell.rightTitleLabel.text = models.title;
        [cell.rightcoverImageView sd_setImageWithURL:[NSURL URLWithString:models.coverimg] placeholderImage:nil];
        cell.rightcontentLabel.text = models.content;
        return cell;

}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        ReadModel *model = self.listArray[indexPath.row];
        ReadDetailContent *content = [[ReadDetailContent alloc] init];
        content.conteneHtml = model.ID;
        [self.navigationController pushViewController:content animated:YES];
    }else{
        
        ReadModel *model = self.rightArray[indexPath.row];
       ReadDetailRightViewController *content = [[ReadDetailRightViewController alloc] init];
        content.conteneHtml = model.ID;
        [self.navigationController pushViewController:content animated:YES];
        
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
